# /usr/bin/env python3.6
# -*- encoding: utf-8 -*-
"""
@File    : ciema_hall_api.py
@Time    : 2020/2/8 0008 11:45
@Author  : zhangbing
@Email   : 18829172841@163.com
@Software: PyCharm
"""
import datetime

from flask import g
from flask_restful import Resource, reqparse, abort, marshal, fields
from sqlalchemy import or_

from App.apis.api_constant import HTTP_CREATE_OK, HTTP_OK
from App.apis.cinema_admin.utils import login_cinema_required
from App.models.cinema_admin.cinema_address_model import CinemaAddress
from App.models.cinema_admin.cinema_hall_model import Hall
from App.models.cinema_admin.cinma_hall_movie_model import HallMovie
from App.models.movie_user.movie_order_model import MovieOrder, ORDER_STATUS_GET, ORDER_STATUS_PAYED_NOT_GET, \
    ORDER_STATUS_NOT_PAY

'''
h_address_id = db.Column(db.Integer, db.ForeignKey(CinemaAddress.id))
h_num = db.Column(db.Integer, default=1)  # 此处不可唯一
h_seats = db.Column(db.String(128))
'''
parse = reqparse.RequestParser()

parse.add_argument("h_num", type=int, required=True, help="请输入要添加的影厅")

parse.add_argument("h_address_id", type=int, required=True, help="请输入要添加的影厅对应的影院")

parse.add_argument("h_seats", type=str, required=True, help="请输入要添加的影厅的编号")

hall_fields = {
    "h_address_id": fields.Integer,
    "h_seats": fields.String,
    "h_num": fields.Integer

}


class CinemaHallsResource(Resource):
    @login_cinema_required
    def post(self):
        args = parse.parse_args()

        h_num = args.get("h_num")
        h_address_id = args.get("h_address_id")
        h_seats = args.get("h_seats")

        # 此处需要判断该用户下的影院下的影院的id，可进行获取获得
        user_id = g.user.id

        address_ids = [cinema.id for cinema in CinemaAddress.query.filter(CinemaAddress.c_user_id == user_id).all()]
        if not h_address_id in address_ids:
            abort(400, msg="该影院不存在于该用户下")
        hall = Hall()

        hall.h_address_id = h_address_id
        hall.h_num = h_num
        hall.h_seats = h_seats

        if not hall.save():
            abort(403, msg="影厅添加失败！！")

        data = {
            "msg": "ok",
            "status": HTTP_CREATE_OK,
            "data": marshal(hall, hall_fields)
        }
        return data


'''
此处用于获取大厅中座位信息,通过排挡ID来获取影厅中购票相关信息
此处需要排除相关其他信息及未支付和支付成功

ORDER_STATUS_NOT_PAY = 0  #订单创建的,未支付的订单

ORDER_STATUS_PAYED_NOT_GET = 1  # 支付失败

ORDER_STATUS_GET = 2  # 支付成功

ORDER_STATUS_TIMEOUT = 3  # 支付超时
'''


class UserMovieHallResource(Resource):
    @login_cinema_required
    def get(self, id):
        hall_movie = HallMovie.query.get(id)
        hall = Hall.query.get(hall_movie.h_hall_id)
        # 此处查询到的是支付成功的和正在支付的订单
        movie_orders_buyed = MovieOrder.query.filter(MovieOrder.o_hall_movie_id == id).filter(
            or_(MovieOrder.o_status == ORDER_STATUS_GET, MovieOrder.o_status == ORDER_STATUS_PAYED_NOT_GET)).all()
        # 此处处理的是未支付及正在创建的订单和支付失效的订单
        movie_orders_rlock = MovieOrder.query.filter(MovieOrder.o_hall_movie_id == id).filter(
            or_(MovieOrder.o_status == ORDER_STATUS_NOT_PAY, MovieOrder.o_time > datetime.datetime.now())).all()
        # 通过 hall_movie_id 获取对应的位置信息
        # MovieOrder 中有o_hall_movie_id 及 hall_movie中的id，中的o_seats 是 hall中的字段

        seats = []
        for movie_orders in movie_orders_buyed:
            sold_seats = movie_orders.o_seats.split("#")
            seats += sold_seats

        for movie_orders in movie_orders_rlock:
            sold_seat = movie_orders.o_seats.split("#")
            seats += sold_seat
        all_seats = hall.h_seats.split("#")  # 此处获取到的是一个列表
        can_buy = set(all_seats) - set(seats)
        hall.h_seats = "#".join(can_buy)
        data = {
            "msg": "ok",
            "status": HTTP_OK,
            "data": marshal(hall, hall_fields)
        }

        return data


if __name__ == "__main__":
    pass
